package com.union.core.sys.action;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hnjowe.util.ComUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.union.core.organization.model.Personnel;
import com.union.core.organization.service.OrganizationParamService;
import com.union.core.organization.service.PersonnelService;
import com.union.core.sys.model.PublicSentAudi;
import com.union.core.sys.model.PublicSentimentModule;
import com.union.core.sys.service.PublicSentAudiService;
import com.union.core.sys.service.PublicSentimentService;
import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysService;
public class PublicSentAudiManagerAction extends ActionSupport{
	/**
	 * 
	 */
	private PublicSentAudi PublicSentAudi;
	private static final long serialVersionUID = -6515956329808958902L;
	private PublicSentAudiService publicSentAudiService = new PublicSentAudiService();
	private PublicSentimentService publicSentimentService = new PublicSentimentService();

	private SysService sysService = SysService.getInstance();
	private static Logger log = Logger.getLogger(FrameAction.class.getName());

	/*评阅列表*/
	private List<PublicSentAudi> pubSentAudis;
	private int managerId;
	private int pubSentId;
	
	/*页面对象*/
	private String startDate;
	private String endDate;
	private String dealType;
	private String queryType;
	private String queryData;
	
	/*舆情模型  */
	private PublicSentimentModule publicSentimentModule;
	//附件上传目录
	private final static String SENTIMENT_DIR = "/upload/sentimentAudi/";
	/*公共对象*/
	private Personnel pers;
	private PersonnelService personSer;
	//日志管理
	private SysLogsService sysLogsSer;
	/*分页设置*/
	private int page = 1;		//页码
	private int intRowCount;	//总行数
	private int intPageCount;	//总页数
	private int start;			//开始
	private int intPageSize = 10;//每页显示的行数
	private int intPage = 1;	//页数变量
	//反馈结果
	private String operaResult;
	//接受单位
	private OrganizationParamService orgSer;
	//权限控制
	private List<String> allPower;

	private List<String> orgNameList;
	public List<String> getAllPower() {
		return allPower;
	}
	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}
	public List<String> getOrgNameList() {
		return orgNameList;
	}
	public void setOrgNameList(List<String> orgNameList) {
		this.orgNameList = orgNameList;
	}
	public String getOperaResult() {
		return operaResult;
	}
	public void setOperaResult(String operaResult) {
		this.operaResult = operaResult;
	}
	public String getDealType() {
		return dealType;
	}
	public void setDealType(String dealType) {
		this.dealType = dealType;
	}
	public String getQueryType() {
		return queryType;
	}
	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getIntRowCount() {
		return intRowCount;
	}
	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}
	public int getIntPageCount() {
		return intPageCount;
	}
	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getIntPageSize() {
		return intPageSize;
	}
	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}
	public int getIntPage() {
		return intPage;
	}
	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getQueryData() {
		return queryData;
	}
	public void setQueryData(String queryData) {
		this.queryData = queryData;
	}

	public int getPubSentId() {
		return pubSentId;
	}
	public void setPubSentId(int pubSentId) {
		this.pubSentId = pubSentId;
	}
	public int getManagerId() {
		return managerId;
	}
	public void setManagerId(int managerId) {
		this.managerId = managerId;
	}
	public String index(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			setPers(sysService.getSessionListener().getPERSONNEL());
			publicSentimentModule=publicSentimentService.getPublicSentimentModuleById(pubSentId);
			PublicSentAudi = new PublicSentAudi();
			PublicSentAudi.setStaff_id(sysService.getSessionListener().getPERSONNEL_ID());
			PublicSentAudi.setOrganization_id(pers.getOrganization_id());
			return "SUCCESS";
		} 
	}
	public Personnel getPers() {
		return pers;
	}
	public void setPers(Personnel pers) {
		this.pers = pers;
	}
	public PublicSentAudi getPublicSentAudi() {
		return PublicSentAudi;
	}
	public void setPublicSentAudi(PublicSentAudi PublicSentAudi) {
		this.PublicSentAudi = PublicSentAudi;
	}
	public PublicSentAudiManagerAction(){
		publicSentAudiService = new PublicSentAudiService();
		personSer=new PersonnelService();
		sysLogsSer=new SysLogsService();
		orgSer=new OrganizationParamService();
	}
	
	/**
	 * 查询列表
	 * @return String
	 */
	public String list(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			setPublicSentimentModule(publicSentimentService.getPublicSentimentModuleById(pubSentId));
			//接收部门列表
			orgNameList=orgSer.getOrgNameList(publicSentimentService.getPublicSentimentModuleById(pubSentId).getReceive_list());
			lookup();
			log.info("查询评阅列表");
			return "SUCCESS";
		}
	} 
	public String initList()
	{
		return SUCCESS;
	}
	
	
	public String add(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			String fileName = "";
			//上传文件
			if(PublicSentAudi.getAfile() != null){
				fileName = ComUtil.genFileId();
				String fileext = ComUtil.getExtention(PublicSentAudi.getAfileFileName());
				fileName = fileName + fileext;
				
				String fullPath = getRealPath() + SENTIMENT_DIR + fileName;
				ComUtil.copyFile(PublicSentAudi.getAfile(), new File(fullPath));
				
				PublicSentAudi.setPubsentm_file(PublicSentAudi.getAfileFileName());
				PublicSentAudi.setFile_path(SENTIMENT_DIR + fileName);
				
			}
			log.info("pubSentId="+pubSentId);
			publicSentimentModule=publicSentimentService.getPublicSentimentModuleById(pubSentId);
			boolean flag = publicSentAudiService.addAudi(pubSentId,PublicSentAudi);
			if(true == flag){
				sysLogsSer.addSysLogs("评阅舆情【"+publicSentimentModule.getPUBSENTM_NAME()+"】");
				operaResult="评阅舆情【"+publicSentimentModule.getPUBSENTM_NAME()+"】成功";
			}else{
				operaResult="评阅舆情【"+publicSentimentModule.getPUBSENTM_NAME()+"】失败";
			} 
			return "SUCCESS";
		}
	}
	
	private void lookup(){
		
		pubSentAudis = publicSentAudiService.getPubSentAudiList(pubSentId, 1);
	}
	
	public String deleteAudi(){
		boolean flag = publicSentAudiService.deleteAudi(pubSentId);
		if(true == flag){
			return "SUCCESS";
		}else{
			return "ERROR";
		} 
	}
	

	public List<PublicSentAudi> getpubSentAudis() {
		return pubSentAudis;
	}
	public void setpubSentAudis(List<PublicSentAudi> pubSentAudis) {
		this.pubSentAudis = pubSentAudis;
	}
	public void setPublicSentimentModule(PublicSentimentModule publicSentimentModule) {
		this.publicSentimentModule = publicSentimentModule;
	}
	public PublicSentimentModule getPublicSentimentModule() {
		return publicSentimentModule;
	}
	public String getRealPath(){
		return ServletActionContext.getServletContext().getRealPath("/");
	}

}
